<?php

namespace App\Models\Admin;

use App\Scopes\SaasScope;
use Illuminate\Database\Eloquent\Model;
use App\User;
use App\Log\DispatchOrderAdminLog;

class DispatchOrder extends Base
{
    const OPERATE_PID_PF = 1;
    const OPERATE_PID = 5;
    const MODEL = 'dispatch_order';

    const ORDER_REVIEW = 0;//订单审核中
    const ORDER_STOCK_UP = 1;//订单备货中
    const ORDER_SHIPPED = 2;//订单已发货
    const ORDER_COMPLETED = 3;//订单已完成
    const ORDER_CANCELLED = 4;//订单已取消

    protected $table = 'dispatch_order';

    protected $dateFormat = 'U';

    public $timestamps = true;

    protected $guarded = [];

    public function orderList()
    {
        return $this->hasMany(DispatchOrderList::class,'oid','id');
    }
    public function orderList_spu()
    {
        return $this->hasMany(DispatchOrderList::class,'oid','id')->with(['goodsSpu']);
    }
    public function orderList_info()
    {
        return $this->hasMany(DispatchOrderList::class,'oid','id')->with(['goodss','goodsSpu']);
    }
    public function orderList_cheack()
    {
        return $this->hasMany(DispatchOrderList::class,'oid','id')->where('return_status','0');
    }

    public function member()
    {
        return $this->hasOne(User::class,'id','dispatch_user_id')->select('id','name')->withoutGlobalScopes();
    }

    public function companyName()
    {
        return $this->hasOne(Company::class,'id','company_uid')->select('id','company_name');
    }

    public function companyNamePf()
    {
        return $this->hasOne(Company::class,'id','company_id')->select('id','company_name');
    }

    public function driver()
    {
        return $this->hasOne(Driver::class,'id','driver_id');
    }


    public function line()
    {
        return $this->hasOne(Line::class,'id','line_id');
    }

    public function salesman()
    {
        return $this->hasOne(SalesMan::class,'id','salesman_id');
    }
    public function address()
    {
        return $this->hasOne(Setting::class,'id','company_uid')->select('id','receive_man','receive_mobile','receive_address');
    }
    /**
     *
     * @param $id        //商品ID
     * @param $status    //状态改变目标值
     * @return []
     */
    public function setStatus($id,$status){
        $info = $this->findOrFail($id);
        if(!$info->id){
            return ['code'=>false,'msg'=>'非法操作'];
        }elseif($info->order_status == $status){
            return ['code'=>false,'msg'=>'不要重复操作'];
        }else{
            $info->order_status = $status;
            $perInfo = $info->getOriginal();
            $result = $info->save();
            $nextInfo = $info->getChanges();
            if (method_exists($this,'addLog')){
                if (!empty($nextInfo)){
                    $this->addLog($perInfo,$nextInfo,6,'setOrderStatus',2);
                }
            }
            return ['code'=>true,'msg'=>'操作成功'];
        }
    }
    public function seach($request){
        $time=$request->query('send_time')?: date("Y-m-d",time());
        $result=$this->whereRaw("FROM_UNIXTIME(send_time,'%Y-%m-%d')='".$time."' and order_status not in(0,4)")->get()->pluck('id');
        return $result;
    }
    public function seachone($send_time,$ids,$company_name){
        $select=$this->with(['member','driver','line','salesman','companyName','orderList_info']);

        if($send_time){
            $arr = explode(' ', $send_time);
            $select=$select->whereBetween('send_time', [strtotime($arr[0]), strtotime($arr[1]) + (24 * 60 * 60 - 1)]);

        }
        if($ids){
            $select=$select->whereIn('id',$ids);
        }
        if($company_name){
            $select= $select->whereIn('company_uid',Company::where('company_name', 'like', "%$company_name%")->where('model','admin')->get()->pluck('id'));
        }

        $result=$select->orderBy('id','desc')->get()->toArray();
        return $result;
    }
}
